package com.code.leetcode.offer;

import com.code.leetcode.utils.TreeNode;

/**
 * @author codeDorado
 * @version 1.0
 * @date 2020/7/31 20:26
 */
public class The27 {
    public TreeNode mirrorTreeO(TreeNode root) {
        if (root == null) return null;
        TreeNode node = new TreeNode(root.val);
        dfs(root, node);
        return node;
    }

    public void dfs(TreeNode root, TreeNode node) {
        if (root == null) return;
        node.right = root.left != null ? new TreeNode(root.left.val) : null;
        node.left = root.right != null ? new TreeNode(root.right.val) : null;
        dfs(root.left, node.right);
        dfs(root.right, node.left);
    }

    public TreeNode mirrorTree(TreeNode root) {
        if (root == null) return null;
        TreeNode temp = root.left;
        root.left = mirrorTree(root.right);
        root.right = mirrorTree(temp);
        return root;
    }
}
